package com.utils.picTxt
{
	public class MsgTranscoding
	{
		private static var _message:XML;
		public function MsgTranscoding()
		{
		}
		//		<rtf>
		//				<text>
		//				  fasfsafsadffdsfdsafs
		//				</text>
		//				<sprites>
		//				  <sprite src="Face_29" index="11"/>
		//				  <sprite src="Face_31" index="20"/>
		//				</sprites>
		//			  </rtf>
		/**XML转字符串*/		
		public static function xmlToStr(message:XML):String
		{
			_message = message;
			var resultStr:String = '';
			var faceIdList:Array = [];
			var faceIndexList:Array = [];
			var messageTxt:String = _message.text[0];
			for(var i:* in _message.sprites.sprite){
				faceIdList.push(_message.sprites.sprite[i].@src);
				faceIndexList.push(_message.sprites.sprite[i].@index);
			}
			resultStr = messageTxt+'/^/^'+faceIdList.join(',') +'/^/^'+faceIndexList.join(',');
			return resultStr;
		}
		
		/**
		 * 设置titleXML
		 * @param params
		 * @param sex 1男 2女
		 * @return 性别 姓名 时间
		 */		
		public static function titleXml(sex:int = -1,...params):XML
		{
			var messageTxt:String = params.join(' ');
			var xml:XML = <rtf><text/><sprites/></rtf>;
			xml.text[0] =  ' ' + messageTxt;
			xml.sprites.sprite[0] = new XML(<sprite></sprite>);
			if(sex != -1){
				xml.sprites.sprite[0].@src = (sex==1?'Face_nan':'Face_nv');
				xml.sprites.sprite[0].@index = 0;
			}
			return xml;
		}
		
		/**字符串转XML*/		
		public static function strToXml(resourceStr:String):XML
		{
			var list:Array = resourceStr.split('/^/^');
			var messageTxt:String = ' ' + list[0];
			var faceIdList:Array = list[1].split(',');
			var faceIndexList:Array =  list[2].split(',');
			var xml:XML = <rtf><text/><sprites/></rtf>;
			xml.text[0] = messageTxt;
			for (var i:int = 0; i < faceIdList.length; i++) 
			{
				if(faceIdList[i] == '') continue;
				xml.sprites.sprite[i] = new XML(<sprite></sprite>);
				xml.sprites.sprite[i].@src = faceIdList[i];
				xml.sprites.sprite[i].@index = int(faceIndexList[i]);
			}
			return xml;
		}
		
		
		
	}
}